#include "distancemodel.h"

DistanceModel::DistanceModel(SystemController *controller, QObject *parent, QSqlDatabase db) : QSqlTableModel(parent, db)
{
    this->systemController = controller;
}


/*!
  \fn selectStatement
  Function which change the selectStatement, to set the order of the columns
  */
QString DistanceModel::selectStatement() const
{
    QString stmt =  QSqlTableModel::selectStatement();
    int wherePos = stmt.lastIndexOf("WHERE");
    int orderPos = stmt.lastIndexOf("ORDER");

    QString newSelect = "Select trackEvent.\"tId\", trackEvent.\"cId\", trackEvent.\"length\", comment.\"name\", trackEvent.\"minAge\", trackEvent.\"maxAge\" from trackEvent left join comment on trackEvent.commentId = comment.commentId ";

    if (wherePos != -1)
    {
        stmt = stmt.remove(0, wherePos);
        stmt = stmt.prepend(newSelect);
    }
    else
    {
        if (orderPos != -1)
        {
            stmt = stmt.remove(0, orderPos);
            stmt = stmt.prepend(newSelect);
        }
        else
            stmt = newSelect;
    }

    stmt = stmt.replace("trackEvent.\"name\"", "comment.\"name\"");
    return stmt;
}
